import 'package:commons/commons.dart';
import 'package:flutter/material.dart';

///国际化类型
enum ILocal {
  ///中文
  zh,

  ///英语
  en;

  factory ILocal.from(String? code) {
    switch (code) {
      case 'en':
        return ILocal.en;
      default:
        return ILocal.zh;
    }
  }

  String get code {
    switch (this) {
      case ILocal.zh:
        return 'zh';
      case ILocal.en:
        return 'en';
      default:
        return 'zh';
    }
  }

  String get name {
    switch (this) {
      case zh:
        return '中文';
      case en:
        return 'English';
      default:
        return '中文';
    }
  }

  Locale get locale {
    switch (this) {
      case zh:
        return const Locale('zh', 'CN');
      case en:
        return const Locale('en', 'US');
      default:
        return const Locale('zh', 'CN');
    }
  }

  Future<void> change() async {
    await Get.updateLocale(locale);
    await Get.storage.set('cc-transition-key', code);
  }

  static ILocal get ls {
    final code = Get.storage.get<String>('cc-transition-key');
    return ILocal.from(code);
  }
}
